package com.outstanding.ifml.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

import com.outstanding.ifml.IFML;
import com.outstanding.ifml.Item;
import com.outstanding.ifml.ItemProperty;
import com.outstanding.ifml.ItemValueClass;
import com.outstanding.ifml.bo.Field;
import com.outstanding.ifml.bo.FieldType;


public class IFMLBOConvertIFML {

	static public IFML convert(List<Field> list) {
		IFML ifml = new IFML();
		int i = 0;
		for (Field field : list) {
			FieldType ft = parseFieldType(field);
			Item rootItem = buildItem(field,ft,0);
			ifml.addIF(rootItem);
		}
		ifml.sort();
		return ifml;

	}
	
	
	
	
	private static FieldType parseFieldType(Field field) {
		if(Field.REQUESTHEADER.equals(field.getName().trim())){
			return FieldType.requestHeader;
		}else if(Field.REQUESTBODY.equals(field.getName().trim())){
			return FieldType.requestBody;
			
		}else if (Field.RESPONSEHEADER.equals(field.getName().trim())){
			return FieldType.responseHeader;
			
		}else if (Field.RESPONSEBODY.equals(field.getName().trim())){
			return FieldType.responseBody;
		}else{
			return FieldType.other;
		}
	}



/**
 * 
 * @param field
 * @param ft
 * @param level
 * @return
 */
	static private Item buildItem(Field field,FieldType ft,int level){
		Item item = new Item(level);
		if(ft != null){
			ItemProperty ip = new ItemProperty();
			ip.setName(Field.FIELDTYPE);
			ip.setValue(ft.getCode());
			item.addProperty(ip);
		}
		item.setName(field.getName());
		if (field.getValue() != null) {
			if (field.getValue().trim().startsWith("[") && field.getValue().trim().endsWith("]")) {
				List<String> arrayValue = new ArrayList<String>();
				String val = field.getValue().trim().substring(1, field.getValue().trim().length() - 2);
				String[] vals = val.split(",");
				vals = rejectQuites(vals);
				item.setValue(vals);
			} else {
				item.setValue(rejectQuites(field.getValue().trim()));
			}
		}
		if(field.getMust() != null){
			ItemProperty ip = new ItemProperty();
			ip.setName(Field.MUST);
			ip.setValue(field.getMust());
			item.addProperty(ip);
		}
		if(field.getDesc() != null){
			ItemProperty ip = new ItemProperty();
			ip.setName(Field.DESC);
			ip.setValue(field.getDesc());
			item.addProperty(ip);
		}
		if(field.getMock() != null ){
			ItemProperty ip = new ItemProperty();
			ip.setName(Field.MOCK);
			ip.setValue(field.getMock());
			item.addProperty(ip);
		}
		if(field.getValueType() != null ){
			item.setItemClass(field.getValueType());
		}else{
			item.setItemClass(ItemValueClass.string);
		}
		List<Field> childfields = field.getChildFields();
		level ++;
		for (Field childfield : childfields) {
			Item childItem = buildItem(childfield,ft,level);
			item.addChildIf(childItem);
		}
		return item;
	}

	static private String rejectQuites(String value) {

		value = rejectStartQuotes(value);
		value = rejectEndQuotes(value);

		return value;
	}

	static private String[] rejectQuites(String[] values) {
		for (String value : values) {
			value = rejectStartQuotes(value);
			value = rejectEndQuotes(value);
		}
		return values;
	}

	/**
	 * 去除引号
	 */
	static private String rejectStartQuotes(String value) {
		if (value.trim().startsWith("\"") || value.trim().startsWith("\'"))
			
			return value.substring(1);
		else
			return value;
	}

	/**
	 * 去除引号
	 */
	static private String rejectEndQuotes(String value) {
		if (value.trim().endsWith("\"") || value.trim().endsWith("\'"))
			return value.substring(0, value.trim().length() - 2);
		else
			return value;
	}
}
